package com.example.health_life.service;

import com.example.health_life.dao.Product;
import com.example.health_life.dao.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public List<Product> findAll() {
        return productRepository.findAll();
    }

    @Override
    public List<Product> findByTitle(String title) {
        return productRepository.findByTitle(title);
    }

    @Override
    public Product findById(Integer id) {
        return productRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("商品不存在"));
    }

    @Override
    public List<Product> findByType(Byte type) {
        return productRepository.findByType(type);
    }

    @Override
    public int addProduct(Product product) {
        if (!productRepository.findByTitle(product.getTitle()).isEmpty()) {
            throw new IllegalStateException("商品已存在");
        }
        // 设置 soldCount 的默认值
        if (product.getSoldCount() == null) {
            product.setSoldCount(0);
        }
        return productRepository.save(product).getId();
    }

    @Override
    public Integer updateProduct(Integer id, Product product) {
        //查找商品是否存在
        Product product1 = productRepository.findById(id).orElseThrow(()->new IllegalStateException("商品不存在"));
        if(product.getTitle() != null){
            product1.setTitle(product.getTitle());
        }
        if(product.getType() != null){
            product1.setType(product.getType());
        }
        if(product.getPrice() != null){
            product1.setPrice(product.getPrice());
        }
        if(product.getCoverImg() != null){
            product1.setCoverImg(product.getCoverImg());
        }
        if(product.getIntroduce() != null){
            product1.setIntroduce(product.getIntroduce());
        }
        if(product.getDetailImages() != null){
            product1.setDetailImages(product.getDetailImages());
        }
        if(product.getCarouselImages() != null){
            product1.setCarouselImages(product.getCarouselImages());
        }
        if(product.getSoldCount() != null)
            product1.setSoldCount(product.getSoldCount());


        productRepository.save(product1);
        return id;
    }

    @Override
    @Transactional
    public String deleteProduct(Integer id) {
        // 检查商品是否存在
        if (!productRepository.existsById(id)) {
            throw new IllegalStateException("商品不存在");
        }
        
        // 执行删除操作
        int deletedCount = productRepository.deleteProductById(id);
        if (deletedCount > 0) {
            return "商品删除成功";
        } else {
            throw new IllegalStateException("商品删除失败");
        }
    }
}
